package com.ljy.my_study.java.juc.CountDownLatch;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import java.util.concurrent.*;

/** 
* @author James
* @date 2018年9月10日 
*/
public class TestMain {
	
	
	public static void main(String[] args) {
		
		int count=1000;
//		ExecutorService executor=Executors.newFixedThreadPool(count);
		ScheduledExecutorService executor=new ScheduledThreadPoolExecutor(100,
				new BasicThreadFactory.Builder().namingPattern("test-%d").daemon(true).build());

		final CountDownLatch latch=new CountDownLatch(count);
		int[] datas=new int[10204];
		int step=datas.length/count;
		for(int i=0;i<count;i++) {
			executor.execute(new Runnable() {
				private CountDownLatch latch=null;
				
				public Runnable init(CountDownLatch latch) {
					this.latch=latch;
					return this;
				}
				
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName()+" runnable run.");
					try {
						Thread.sleep(1L);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					latch.countDown();
					System.out.println(Thread.currentThread().getName()+" runnable run end.");
				}
			}.init(latch));
		}
		try {
			Thread.sleep(500);
			System.out.println("main run.");
			latch.await();
			System.out.println("main run end.");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		executor.shutdown();
	}
}
